package nus.iss.laps;

import java.sql.SQLException;
import java.util.List;
import java.util.Locale;

import nus.iss.laps.cri.Criteria;
import nus.iss.laps.dao.LeavesentitlementDAO;
import nus.iss.laps.model.Leavesentitlement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

@Controller
public class EntitlementController 
{
	@Autowired
	LeavesentitlementDAO leavesentitlementDAO;

	@RequestMapping(value = "/entitlement", method = RequestMethod.GET)
	public String leavesEntitlementList(Model model, WebRequest webRequest)
	{
		List<Leavesentitlement> entitlementList;
		try {
			Criteria l_cri = new Criteria();
			l_cri.setRowIndex(0);
			l_cri.setRowLimit(5);
			entitlementList = leavesentitlementDAO.getAllLeavesentitlement();
			model.addAttribute("entitlementList", entitlementList);
			model.addAttribute("criteria", l_cri);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		
	   return "show_leaves_entitlement";
	}
	
	@RequestMapping(value = "/entitlement/new", method = RequestMethod.GET)
	public String newEntitlement(Locale locale, Model model)
	{
		model.addAttribute("leavesentitlement", new Leavesentitlement());
		return "leaves_entitlement";
	}
	
	@RequestMapping(value = "/entitlement/{entitlementID}", method = RequestMethod.GET)
	public String viewHoliday(Locale locale, Model model, @PathVariable int entitlementID)
	{
		try {
			Leavesentitlement l_leavesentitlement = leavesentitlementDAO.getLeavesentitlement(entitlementID);
			model.addAttribute("leavesentitlement",  l_leavesentitlement);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "leaves_entitlement";
	}
	
	@RequestMapping(value = "/entitlement", method = RequestMethod.POST)
	public String rowLimitChange(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setKeyword(webRequest.getParameter("txtKeyword"));
			l_cri.setRowIndex(0);
			l_cri.setRowLimit(Integer.parseInt(webRequest.getParameter("cboRows")));
			
			List<Leavesentitlement> l_entitlementList = leavesentitlementDAO.getLeavesentitlement(l_cri);
			model.addAttribute("entitlementList", l_entitlementList);
			model.addAttribute("criteria", l_cri);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "show_leaves_entitlement";
	}
	
	@RequestMapping(value = "/entitlement", method = RequestMethod.POST, params = "btnPrev")
	public String previous(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setKeyword(webRequest.getParameter("txtKeyword"));
			l_cri.setRowLimit(Integer.parseInt(webRequest.getParameter("cboRows")));
			l_cri.setRowIndex(Integer.parseInt(webRequest.getParameter("rowIndex")) - l_cri.getRowLimit());
			List<Leavesentitlement> l_entitlementList = leavesentitlementDAO.getLeavesentitlement(l_cri);
			model.addAttribute("criteria", l_cri);
			model.addAttribute("entitlementList", l_entitlementList);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "show_leaves_entitlement";
	}
	
	@RequestMapping(value = "/entitlement", method = RequestMethod.POST, params = "btnNext")
	public String next(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setKeyword(webRequest.getParameter("txtKeyword"));
			l_cri.setRowLimit(Integer.parseInt(webRequest.getParameter("cboRows")));
			l_cri.setRowIndex(Integer.parseInt(webRequest.getParameter("rowIndex")) + l_cri.getRowLimit());
			List<Leavesentitlement> l_entitlementList = leavesentitlementDAO.getLeavesentitlement(l_cri);
			model.addAttribute("criteria", l_cri);
			model.addAttribute("entitlementList", l_entitlementList);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "show_leaves_entitlement";
	}
	
	@RequestMapping(value = "/entitlement/*", method = RequestMethod.POST, params = "save")
	public String saveLeavesentitlement(Model model, @ModelAttribute("SpringWeb")Leavesentitlement leavesentitlement)
	{
				try {
					leavesentitlementDAO.addLeavesentitlement(leavesentitlement);
				} catch (SQLException ex) {
					model.addAttribute("error", ex.getMessage());
					return "error";
				}
		return "redirect:/entitlement";
	}
	
	@RequestMapping(value = "/entitlement/*", method = RequestMethod.POST, params = "edit")
	public String editHoliday(Model model, @ModelAttribute("SpringWeb")Leavesentitlement leavesentitlement)
	{
		try {
			leavesentitlementDAO.updateLeavesentitlement(leavesentitlement);
			
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "redirect:/entitlement";
	}
	
	@RequestMapping(value = "/entitlement/*", method = RequestMethod.POST, params = "delete")
	public String deleteHoliday(Model model, WebRequest webRequest)
	{
		try {
			leavesentitlementDAO.deleteLeavesentitlement(Integer.parseInt(webRequest.getParameter("entitlementID")));
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "redirect:/entitlement";
	}
	
}
